//
//  FooterView.swift
//  ScrollViewRefresh
//
//  Created by mac on 15/3/13.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit

class FooterView: BaseView {
    weak var statusView: StatusView?
    var targetValue: CGFloat = 60
    var block: (() -> ())?
    
    override func setUpSubViews() {
        let statusView = StatusView()
        self.addSubview(statusView)
        self.statusView = statusView
        statusView.refreshingText = "数据正在刷新中..."
        statusView.backTransform =  CGAffineTransformIdentity
        statusView.startTransform = CGAffineTransformMakeRotation( CGFloat(M_PI) )
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        statusView!.frame = self.bounds
    }
    
    override func setUpObserver() {
        newSuperview?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
        if statusView!.state == .Loading { return }
        let difference = newSuperview!.contentOffset.y + newSuperview!.bounds.height - self.frame.origin.y
        if  difference > targetValue {
            if !newSuperview!.dragging {
                beginFooterRefresh()
            }
            if statusView!.state != .RotateBack && statusView!.state != .Loading{
                statusView!.state = .RotateBack
            }
        } else if difference < targetValue {
            // 在拖拽的状态下根据位移调整箭头的方向
            if newSuperview!.dragging && statusView!.state != .Normal{
                statusView!.state = .Normal
            }
        }
    }
    
    func beginFooterRefresh() {
        if statusView!.state == .Loading {
            return
        }
        statusView!.state = Status.Loading
        BaseView.originInset.bottom += self.frame.size.height
        newSuperview!.contentInset = BaseView.originInset
        // 如果没有滚动到足够的位置就不需要设定 contentOffset
        if block != nil {
            block!()
        }
        var offset = newSuperview!.contentOffset
        if (offset.y + newSuperview!.bounds.height ) < self.frame.origin.y - self.frame.size.height { return }
        offset.y = newSuperview!.contentSize.height - newSuperview!.bounds.height  + self.frame.size.height
        newSuperview!.setContentOffset(offset, animated: true)
    }
    
    func endFooterRefresh() {
        if statusView!.state == .Normal { return }
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            BaseView.originInset.bottom -= self.frame.size.height
            self.newSuperview!.contentInset = BaseView.originInset
            var offset = self.newSuperview!.contentOffset
            if (offset.y + self.newSuperview!.bounds.height ) < self.frame.origin.y - self.frame.size.height { return }
            offset.y = self.frame.origin.y - self.newSuperview!.bounds.height
            self.newSuperview?.setContentOffset(offset, animated: false)
            }) { (complete) -> Void in
                self.statusView!.state = .Normal
        }
    }


}
